//
//  Home_TypeView.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/1.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "Home_TypeView.h"
#import "LabCell.h"
#import "LotteryTypeModel.h"
#import "NewsLottryTypeModel.h"
#import "PlanModel.h"
#import "GalleryModel.h"
#import "PlayRuleModel.h"
@interface Home_TypeView()<UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic ,strong) UICollectionView *collectionView;
@end

@implementation Home_TypeView{
    NSArray *typeArr;
}

- (instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=[UIColor colorWithWhite:0.0 alpha:.3];
        UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remove)];
        tap1.delegate=self;
        [self addGestureRecognizer:tap1];
    }
    return self;
}


- (void)setScrollIndex:(NSInteger)scrollIndex{
    
    if (typeArr.count==0) {
        return;
    }
    
    _selectType = scrollIndex;
    [self layoutIfNeeded];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:scrollIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
}


- (void)setArr:(NSArray *)arr andIndex:(NSInteger)index andSelectBlock:(SelectBlock)select{
    self.selectBlock = select;
    typeArr=arr;
    _selectType = 0;
    
    WS(weakSelf);
    UIView *mainView=[[UIView alloc]init];
    mainView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remove)];
    tap.delegate=self;
    [mainView addGestureRecognizer:tap];
    mainView.backgroundColor=[UIColor whiteColor];
    [self addSubview:mainView];
    
    if (46*ceilf(arr.count/4.0)+10>SCREEN_HEIGHT-164) {
        [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.top.equalTo(weakSelf);
        }];
    }else{
        [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(weakSelf);
            
            make.height.mas_equalTo(@(46*ceilf(arr.count/4.0)+40));
        }];
    }
    
    QXLable *lab=[[QXLable alloc]initTitle];
    id model=arr[index];
    if ([model isKindOfClass:[PlayRuleModel class]]) {
        PlayRuleModel *pModel=model;
        lab.text=pModel.Name;
    }
    
    [mainView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainView).with.offset(10);
        make.centerX.equalTo(mainView);
    }];
    
    [mainView addSubview:self.collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(mainView);
        make.left.equalTo(mainView).with.offset(13);
        make.right.equalTo(mainView).with.offset(-13);
        make.top.equalTo(lab.mas_bottom).with.offset(10);
    }];
    
    UIView  *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [mainView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(mainView);
        make.height.mas_equalTo(@.7);
    }];
    
   
    
    [self setScrollIndex:index];
    
//    _selectType=index;
    
}

- (void)setNOTitleArr:(NSArray *)arr andIndex:(NSInteger)index andSelectBlock:(SelectBlock)select{
    self.selectBlock = select;
    typeArr=arr;
    _selectType=index;
    WS(weakSelf);
    UIView *mainView=[[UIView alloc]init];
    mainView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remove)];
    tap.delegate=self;
    [mainView addGestureRecognizer:tap];
    mainView.backgroundColor=[UIColor whiteColor];
    [self addSubview:mainView];
    if (46*ceilf(arr.count/4.0)+10>SCREEN_HEIGHT-64) {
        [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.top.equalTo(weakSelf);
        }];
    }else{
        [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(weakSelf);
            
            make.height.mas_equalTo(@(46*ceilf(arr.count/4.0)+10));
        }];
    }
    
    
    
    [mainView addSubview:self.collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(mainView);
        make.left.top.equalTo(mainView).with.offset(13);
        make.right.equalTo(mainView).with.offset(-13);
    }];
    
    
}

- (void)setSelectType:(NSInteger)selectType{
    _selectType=selectType;
    
    [self.collectionView reloadData];
}

- (void)remove{
    if (self.selectBlock) {
        self.selectBlock(_selectType);
    }
    
    [self removeFromSuperview];
}


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.collectionView]) {
        return NO;
    }
    return YES;
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return typeArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    if ([typeArr[indexPath.row] isKindOfClass:[LotteryTypeModel class]]) {
        LotteryTypeModel *model=typeArr[indexPath.row];
        [cell setBGColorWithTitle:model.Name andIsSelect:indexPath.row==_selectType];
    }else if ([typeArr[indexPath.row] isKindOfClass:[NewsLottryTypeModel class]]){
        NewsLottryTypeModel *model=typeArr[indexPath.row];
        [cell setBGColorWithTitle:model.LTypeName andIsSelect:indexPath.row==_selectType];
        cell.labTitle.font=[UIFont systemFontOfSize:12];
    }else if ([typeArr[indexPath.row] isKindOfClass:[NewsTypeModel class]]){
        NewsTypeModel *model=typeArr[indexPath.row];
        [cell setBGColorWithTitle:model.TypeName andIsSelect:indexPath.row==_selectType];
    }else if ([typeArr[indexPath.row] isKindOfClass:[PlanModel class]]){
        PlanModel *model=typeArr[indexPath.row];
        [cell setBGColorWithTitle:model.LTypeName andIsSelect:indexPath.row==_selectType];
    }else if ([typeArr[indexPath.row] isKindOfClass:[GalleryExtraModel class]]){
        GalleryExtraModel *model=typeArr[indexPath.row];
        [cell setBGColorWithTitle:[NSString stringWithFormat:@"第%@期",model.Issue] andIsSelect:indexPath.row==_selectType];
    }else if ([typeArr[indexPath.row] isKindOfClass:[PlayRuleModel class]]){
        PlayRuleModel *model=typeArr[indexPath.row];
        [cell setLinColorWithTitleWithTitle:[NSString stringWithFormat:@"%@",model.Name] andIsSelect:indexPath.row==_selectType];
    }
    else{
        [cell setBGColorWithTitle:typeArr[indexPath.row] andIsSelect:indexPath.row==_selectType];
    }
    
    
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _selectType=indexPath.row;
    [self remove];
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-26*3)/4.0, 30)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 13.0;
        layout.minimumLineSpacing = 13.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        _collectionView.userInteractionEnabled=YES;
        
    }
    return _collectionView;
}

@end
